package org.lmw.note;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.frontia.Frontia;
import com.baidu.frontia.FrontiaData;
import com.baidu.frontia.FrontiaQuery;
import com.baidu.frontia.api.FrontiaStorage;
import com.baidu.frontia.api.FrontiaStorageListener;
import com.baidu.frontia.api.FrontiaStorageListener.DataInfoListener;
import com.baidu.frontia.api.FrontiaStorageListener.DataOperationListener;
import com.google.gson.Gson;
import com.norbsoft.typefacehelper.TypefaceHelper;

public class Act_Cloud extends FragmentActivity {
	
	FrontiaStorage mCloudStorage = Frontia.getStorage();
	TextView result;
	Button reg; 
	Button btn;
	Button del;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.act_cloud);
		TypefaceHelper.typeface(this);
		result=(TextView)findViewById(R.id.textView1);
		btn=(Button)findViewById(R.id.button1);
		reg=(Button)findViewById(R.id.button2);
		del=(Button)findViewById(R.id.button3);
		del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				delData();
			}
		});
		
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				queryData();
			}
		});
		
		reg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				upData();
			}
		});
	}
	
	//注册
	private void upData(){
		FrontiaData data = new FrontiaData();
		User u=new User();
		u.setUserName("15978890373");
		u.setPassWord("id984461223");
		List<Note> ns=new ArrayList<Note>();
		
		JSONObject obj=new JSONObject();
		try {
		obj.put("user", new Gson().toJson(u));
		obj.put("notes", new Gson().toJson(ns));
		} catch (JSONException e) {
			e.printStackTrace();
			return;
		}
		data.putAll(obj);
		mCloudStorage.insertData(data,
		  new FrontiaStorageListener.DataInsertListener() { 
		    public void onSuccess() {
		    	Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
		    }
		    public void onFailure(int errCode, String errMsg) {
		    	Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_SHORT).show();
		    }
		  });
	}
	
	//登录
	private void queryData(){
//		FrontiaQuery q1=new FrontiaQuery();
//		q1.equals("userName", "15978890373");
//		FrontiaQuery q2=new FrontiaQuery();
//		q2.equals("passWord", "id984461223");
//		FrontiaQuery query=new FrontiaQuery();
//		query=q1.and(q2);
		User u=new User();
		u.setUserName("15978890373");
		u.setPassWord("id984461223");
		
		FrontiaQuery query=new FrontiaQuery();
		query.equals("user",new Gson().toJson(u));
		
		mCloudStorage.findData(query, new DataInfoListener() {
			@Override
			public void onSuccess(List<FrontiaData> res) {
//				if(res.size()==1){
//					result.setText(new Gson().toJson(res));
//				}
				result.setText(new Gson().toJson(res));
				System.out.println(new Gson().toJson(res));
			}
			
			@Override
			public void onFailure(int arg0, String msg) {
				result.setText(new Gson().toJson(msg));
				Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	
	private void delData(){
//		FrontiaQuery q1=new FrontiaQuery();
//		q1.equals("userName", "15978890373");
//		FrontiaQuery q2=new FrontiaQuery();
//		q2.equals("passWord", "id984461223");
//		FrontiaQuery query=new FrontiaQuery();
//		query=q1.and(q2);
		FrontiaQuery query=new FrontiaQuery();
		query.equals("a");
		mCloudStorage.deleteData(query, new DataOperationListener() {
			
			@Override
			public void onSuccess(long arg0) {
				System.out.println("del success"+arg0);
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				System.out.println("del failed"+arg1);
			}
		});
	}
}
